package com.boe.csb.core.common.exception;

import org.apache.commons.lang3.StringUtils;

/**
 * Created by louie on 16-11-16.
 */
public class CSBException extends RuntimeException {

    private String errorMessage;
    private CSBErrorCode error;


    public CSBException(CSBErrorCode error, String errorMessage) {
        super(buildErrorInfo(error, errorMessage));
        this.error = error;
        
        setErrorMessage(error.getMessage(), errorMessage);
    }

    public CSBException(CSBErrorCode error, String errorMessage, Throwable cause) {
        super(buildErrorInfo(error, errorMessage), cause);
        this.error = error;
        setErrorMessage(error.getMessage(), errorMessage);
    }

    public CSBException(CSBErrorCode error, Throwable cause) {
        super(buildErrorInfo(error, error.getMessage()), cause);
        this.error = error;
        this.errorMessage = error.getMessage();
    }
    
    private void setErrorMessage(String codeMsg, String errorMessage){
    	if(StringUtils.isEmpty(errorMessage)){
    		this.errorMessage = error.getMessage();
        }else{
         	this.errorMessage = error.getMessage() + "," + errorMessage;
        }
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public CSBErrorCode getError() {
        return error;
    }

    private static String buildErrorInfo(CSBErrorCode errorCode, String errorMessage) {
        return "{errorCode=" + errorCode.getCode() +
                ", errorMessage=" + errorMessage + "}";
    }

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

}
